<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">
            <el-row type="flex" justify="center">
              <el-form ref="form" :model="formData" :rules="rules" style="width: 50%" label-width="120px">
                <el-form-item label="姓名" prop="username">
                  <el-input v-model="formData.username" />
                </el-form-item>
                <el-form-item label="密码" prop="password">
                  <el-input v-model="formData.password" type="password" clearable />
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" @click="updateFn">更新</el-button>
                </el-form-item>
              </el-form>
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <UserInfo />
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <JobInfo />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>
<script>
import { employeesInfo, saveUser } from '@/api/employees'
import UserInfo from './componeets/user-info.vue'
import JobInfo from './componeets/job-info.vue'
export default {
  components: { UserInfo, JobInfo },
  data() {
    return {
      formData: {
        username: '',
        password: ''
      },
      userId: this.$route.params.id, // 员工id
      rules: {
        username: [
          { required: true, message: '用户名不能为空！', trigger: 'blur' },
          { min: 1, max: 4, message: '用户名长度1到4位!', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空！', trigger: 'blur' }
        ]
      }
    }
  },
  async created() {
    // 获取用户信息
    const res = await employeesInfo(this.userId)
    // 保存用户名 实现回显
    this.formData = res.data.data
    this.formData.password = ''
  },
  methods: {
    // 更新用户信息
    updateFn() {
      // 表单校验
      this.$refs.form.validate(async bool => {
        if (bool) { // bool 值是布尔值，true表示校验通过 否则不通过
          // 调用接口 实现更新
          await saveUser(this.formData)
        }
      })
    }
  }
}
</script>
  <style scoped>
  </style>

